package personnel.department.pages.addpage;

import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.jface.viewers.TableViewer;

import com.google.common.collect.Lists;

import personnel.department.dao.entity.Degree;
import personnel.department.dao.entity.Diplom;
import personnel.department.pages.PDTable;
import personnel.department.pages.widgets.AbstractWidget;

public class DegreeEditor extends AbstractTableEditor<Degree> {

	public DegreeEditor(CTabFolder tabFolder) {
		super(tabFolder, "Ученая степень");
	}
	
	public DegreeEditor(CTabFolder tabFolder, Set<Degree> degrees) {
		super(tabFolder, "Ученая степень", degrees);
	}

	@Override
	public PDTable<Degree> createTable(Composite parent) {
		return new PDTable<Degree>(parent) {
			@Override
			public List<Degree> setModelProvider() {
				return getModel();
			}

			@Override
			protected void createColumns(Composite parent, TableViewer viewer) {
				String[] titles = { "Серия", "Номер диплома", "Период обучения", "Дата присвоения", "К.м.н.", "Д.м.н."};
			    int[] bounds = { 100, 130, 130, 130, 80, 80};

			    TableViewerColumn col = createTableViewerColumn(getViewer(), titles[0], bounds[0], 0);
			    col.setLabelProvider(new ColumnLabelProvider() {
			      @Override
			      public String getText(Object element) {
			       Degree e = (Degree) element;
			        return String.valueOf(e.getDiplom().getSeries());
			      }
			    });

			    col = createTableViewerColumn(getViewer(), titles[1], bounds[1], 1);
			    col.setLabelProvider(new ColumnLabelProvider() {
			      @Override
			      public String getText(Object element) {
			    	  Degree e = (Degree) element;
				        return String.valueOf(e.getDiplom().getDiplomNumber());
			      }
			    });

			    col = createTableViewerColumn(getViewer(), titles[2], bounds[2], 2);
			    col.setLabelProvider(new ColumnLabelProvider() {
			      @Override
			      public String getText(Object element) {
			    	  Degree e = (Degree) element;
			    	  Diplom diplom = e.getDiplom();
				        return format.format(diplom.getStartEducation()) + " - " + format.format(diplom.getFinishEducation());
			      }
			    });
			    
			    col = createTableViewerColumn(getViewer(), titles[3], bounds[3], 3);
			    col.setLabelProvider(new ColumnLabelProvider() {
			    	@Override
			    	public String getText(Object element) {
				    	  Degree e = (Degree) element;
					        return format.format(e.getDateOfAssigment());
			    	}
			    });
			    
			    col = createTableViewerColumn(getViewer(), titles[4], bounds[4], 4);
			    col.setLabelProvider(new ColumnLabelProvider() {
			    	@Override
			    	public String getText(Object element) {
			    		Degree e = (Degree) element;
			    		return String.valueOf(e.getKmn());
			    	}
			    });
			    
			    col = createTableViewerColumn(getViewer(), titles[5], bounds[5], 5);
			    col.setLabelProvider(new ColumnLabelProvider() {
			    	@Override
			    	public String getText(Object element) {
			    		Degree e = (Degree) element;
			    		return String.valueOf(e.getDmn());
			    	}
			    });

			}
		};
	}

	@Override
	protected void validate() {
		AbstractWidget<?> kmn = getWidgetByFieldName("kmn");
		AbstractWidget<?> dmn = getWidgetByFieldName("dmn");
		Set<Degree> degrees = getAllInstances();
		degrees.stream().forEach(degree -> {
			disabledDegree(kmn, dmn, degree);
		});
	}

	private void disabledDegree(AbstractWidget<?> kmn, AbstractWidget<?> dmn,
			Degree degree) {
		if (degree.getDmn()) {
			kmn.setEnabled(false);
			dmn.setEnabled(false);
		} else if (degree.getKmn()) {
			kmn.setEnabled(false);
		}
	}

}
